package com.wyg.demo.juc;

import com.wyg.demo.common.CommonResult;
import com.wyg.demo.juc.config.MyThreadConfig;
import com.wyg.demo.service.CompletableFutureService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author WenYanguang
 * @since 2023/3/19
 */
@RestController
public class FutureThreadPoolDemo {
    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;
    @Autowired
    private CompletableFutureService completableFutureService;

    @PostMapping("/execute/thread")
    public CommonResult<Object> m(){
        List<String> threadList = new ArrayList<>();
        for (int i = 0; i < 1000; i++) {
            threadPoolExecutor.execute(
                    () -> threadList.add(Thread.currentThread().getName())
            );
        }
        threadPoolExecutor.shutdown();
        return CommonResult.success("执行完毕", threadList);
    }

    @PostMapping("/execute/completable")
    public CommonResult<Object> completable(){
        List<String> threadList = new ArrayList<>();
        completableFutureService.execute(threadList);
        return CommonResult.success("执行完毕", threadList);
    }
}
